package com.demo.servlet.demo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author apple
 */
@WebServlet(name = "helloServlet", value = "/hello")
public class HelloHttpServlet extends HttpServlet {
    private String message;

    @Override
    public void init() {
        message = "Hello World111112222222222! 测试中文";
    }

    /**
     * // hello?username=ddone&password=456
     * @param request
     * @param response
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
       doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
        request.getRequestDispatcher("success.html").forward(request,response);
    }

    @Override
    public void destroy() {
    }
}